package com.zzyl.nursing.controller.member;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.zzyl.common.core.domain.R;
import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.dto.FamilyDTO;
import com.zzyl.nursing.dto.QueryDeviceDataByTimeDto;
import com.zzyl.nursing.dto.UserLoginRequestDto;
import com.zzyl.nursing.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.nursing.domain.FamilyMember;
import com.zzyl.nursing.service.IFamilyMemberService;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.common.core.page.TableDataInfo;

/**
 * 老人家属Controller
 *
 * @author ruoyi
 * @date 2025-07-31
 */
@RestController
@RequestMapping("/member/user")
@Api(tags = "老人家属相关接口")
public class FamilyMemberController extends BaseController {
    @Autowired
    private IFamilyMemberService familyMemberService;

    /**
     * 查询老人家属列表
     */
    //@PreAuthorize("@ss.hasPermi('nursing:member:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取老人家属列表")
    public TableDataInfo<List<FamilyMember>> list(FamilyMember familyMember) {
        startPage();
        List<FamilyMember> list = familyMemberService.selectFamilyMemberList(familyMember);
        return getDataTable(list);
    }

    /**
     * 导出老人家属列表
     */
    //@PreAuthorize("@ss.hasPermi('nursing:member:export')")
    @Log(title = "老人家属", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出老人家属列表")
    public void export(HttpServletResponse response, FamilyMember familyMember) {
        List<FamilyMember> list = familyMemberService.selectFamilyMemberList(familyMember);
        ExcelUtil<FamilyMember> util = new ExcelUtil<FamilyMember>(FamilyMember.class);
        util.exportExcel(response, list, "老人家属数据");
    }

    /**
     * 获取老人家属详细信息
     */
    //@PreAuthorize("@ss.hasPermi('nursing:member:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取老人家属详细信息")
    public R<FamilyMember> getInfo(@ApiParam(value = "老人家属ID", required = true)
                                   @PathVariable("id") Long id) {
        return R.ok(familyMemberService.selectFamilyMemberById(id));
    }

    /**
     * 新增老人家属
     */
    //@PreAuthorize("@ss.hasPermi('nursing:member:add')")
    //@Log(title = "老人家属", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation("新增老人家属")
    public AjaxResult add(@ApiParam(value = "老人家属实体", required = true) @RequestBody FamilyDTO dto) {
        familyMemberService.insertFamilyMember(dto);
        return success();
    }

    /**
     * 修改老人家属
     */
    //@PreAuthorize("@ss.hasPermi('nursing:member:edit')")
    @Log(title = "老人家属", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改老人家属")
    public AjaxResult edit(@ApiParam(value = "老人家属实体", required = true) @RequestBody FamilyMember familyMember) {
        return toAjax(familyMemberService.updateFamilyMember(familyMember));
    }

    /**
     * 删除老人家属
     */
    //@PreAuthorize("@ss.hasPermi('nursing:member:remove')")
    @Log(title = "老人家属", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除老人家属")
    public AjaxResult remove(@ApiParam(value = "要删除的数据id的数组") @PathVariable Long[] ids) {
        return toAjax(familyMemberService.deleteFamilyMemberByIds(ids));
    }

    /**
     * 微信小程序登录功能
     *
     * @param dto
     * @return
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody UserLoginRequestDto dto) {
        LoginVo loginVo = familyMemberService.login(dto);
        return success(loginVo);
    }


    /**
     * 查询家人列表
     *
     * @return
     */
    @GetMapping("/my")
    public AjaxResult searchFamilyMember() {
        List<FamilyMemberElderVO> list = familyMemberService.searchFamilyMember();
        return success(list);
    }

    /**
     * 查看家人列表(分页查询)
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list-by-page")
    public AjaxResult listByPage(Integer pageNum, Integer pageSize) {
        startPage();
        List<FamilyMemberElderAndDeviceVO> list = familyMemberService.listByPage();
        return success(list);
    }


    /**
     * 查询健康数据
     *
     * @param dto
     * @return
     */
    @PostMapping("/QueryDevicePropertyStatus")
    public AjaxResult QueryDevicePropertyStatus(@RequestBody DeviceDto dto) {
        ResponseListVo responseListVo = familyMemberService.queryDevicePropertyStatus(dto);
        return success(responseListVo);
    }

    /**
     * 按天统计查询指标数据
     *
     * @param
     * @return
     */
    @GetMapping("/queryDeviceDataListByDay")
    public AjaxResult queryDeviceDataListByDay(@RequestParam String functionId, @RequestParam Long startTime, @RequestParam Long endTime, @RequestParam String iotId, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) {
        List<DeviceDataByTimeVo> list = familyMemberService.queryDeviceDataListByDay(functionId, startTime, endTime, iotId);
        return success(list);
    }

    /**
     * 按周统计查询指标数据
     *
     * @param
     * @return
     */
    @GetMapping("/queryDeviceDataListByWeek")
    public AjaxResult queryDeviceDataListByWeek(@RequestParam String functionId, @RequestParam Long startTime, @RequestParam Long endTime, @RequestParam String iotId, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) {
        List<DeviceDataByTimeVo> list = familyMemberService.queryDeviceDataListByWeek(functionId, startTime, endTime, iotId);
        return success(list);
    }

    /**
     * 根据id解绑家人
     * @param id
     * @return
     */
    @DeleteMapping("/deleteById")
    public AjaxResult deleteById(@RequestParam Long id){
        familyMemberService.deleteById(id);
        return success();
    }

    @GetMapping("/pageQueryAlertData")
    public void test(){
    }
}
